package com.phpxiu.app.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.phpxiu.app.emotion.EmotionIcon;
import com.phpxiu.app.kkylive.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 吴金洲 on 2016/7/29
 */
public class EmotionPagerAdapter extends PagerAdapter implements AdapterView.OnItemClickListener{
    private Context mContext;
    private LayoutInflater mInflater;
    private List<List<EmotionIcon>>pages=new ArrayList<List<EmotionIcon>>();
    private OnEmotionInputCallBack mCallBack;
    public EmotionPagerAdapter(Context context, List<List<EmotionIcon>> pages,OnEmotionInputCallBack callBack) {
        this.mContext = context;
        this.pages = pages;
        mInflater=LayoutInflater.from(context);
        mCallBack=callBack;
    }

    @Override
    public int getCount() {
        return pages.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        RelativeLayout pageView=(RelativeLayout)mInflater.inflate(R.layout.c2c_chat_emotion_grid_view,null);
        GridView gridView=(GridView)pageView.findViewById(R.id.c2c_chat_emtion_grid_view);
        gridView.setOnItemClickListener(this);
        gridView.setGravity(Gravity.CENTER);
        EmotionGridAdapter adapter=new EmotionGridAdapter(mContext,pages.get(position));
        gridView.setAdapter(adapter);
        container.addView(pageView);
        return pageView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(view.getTag()!=null&& view.getTag() instanceof EmotionIcon){
            EmotionIcon e=(EmotionIcon)view.getTag();
            if(mCallBack!=null){
                mCallBack.onEmotionInput(e);
            }
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    public interface OnEmotionInputCallBack {
        void onEmotionInput(EmotionIcon emotion);
    }
}